#include <stdio.h>
#include <unistd.h>
#include <mutex>

class Object {
public:
    static Object* GetInstance()
    {
        static Object instance("instance");
        return &instance;
    }

    Object(const char *name)
    {
        pname = name;
        mutex = new std::mutex();
        printf("+ %s\n", pname);
    }

    ~Object()
    {
        delete mutex;
        mutex = nullptr;
        printf("- %s\n", pname);
    }

    void Func()
    {
        std::lock_guard<std::mutex> lock(*mutex);
        printf("* %s %p\n", pname, mutex);
    }

private:
    const char *pname;
    std::mutex *mutex;
};

Object a1("a1");
static Object a2("static a2");
Object a3("a3");
static Object a4("static a4");

class B : public Object {
public:
    B(const char *pname) : Object(pname)
    {
    }
    ~B()
    {
        Object::GetInstance()->Func();
    }
};

void func()
{
    printf("+ %s\n", __func__);

    B b("b");
    Object d1("d1");
    static Object d2("static d2");
    Object d3("d3");
    static Object d4("static d4");

    printf("- %s\n", __func__);
}

static B b("static b");

int main()
{
    printf("+ %s\n", __func__);

    func();

    exit(0);

    Object b1("b1");
    static Object b2("static b2");
    Object b3("b3");
    static Object b4("static b4");

    func();

    printf("- %s\n", __func__);
    return 0;
}
